﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TinyPG
{
    public partial class Codage : Form
    {
        private bool BoolAffichage = true;
        private Dessinateur draw = new Dessinateur();

        public Codage()
        {
            InitializeComponent();
            comboBox1.DataSource = Document.Doc.CartesArd;
            buttonAff.Enabled = false;
        }



        private void button1_Click(object sender, EventArgs e)
        {
            // Création d'un nouveau document qui stockera l'ensemble des informations   
            Document.Doc = new Document();

            Scanner scanner = new Scanner();

            Parser parser = new Parser(scanner);

            ParseTreeEval treeB = new ParseTreeEval();

            ParseTree tree = parser.Parse(textBox1.Text, treeB);


            tree.Eval(null).ToString();
            textBox2.Text = Document.Doc.ToString();
            //textBox2.SelectAll();
            //textBox2.Copy();

            Document.Doc.Structurer();
            buttonAff.Enabled = true;

            BoolAffichage = true;


        }

        private void Form1_Load(object sender, EventArgs e)
        {

            buttonAff.Text = "Cacher";
            if (File.Exists("lastCode.txt"))
            {
                textBox1.Text = File.ReadAllText("lastCode.txt");
            }
            else if (File.Exists("code.txt"))
            {
                textBox1.Text = File.ReadAllText("code.txt");
            }
            else
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();

                openFileDialog1.InitialDirectory = "c:\\";
                openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text = File.ReadAllText(openFileDialog1.FileName);
                }
                else
                {
                    textBox1.Text = "";
                    textBox1.Text += "Etape e0:INIT e1\r\n";
                    textBox1.Text += "Entree I1(PC.5):HI I2(PC.4):HI\r\n";
                    textBox1.Text += "Sortie O1(PD.6) O2(PD.4)\r\n";
                    textBox1.Text += "Timer T1/e0/200\r\n";
                    textBox1.Text += "Receptivite \r\n";
                    textBox1.Text += "r1 =! I1 & T1.OFF\r\n";
                    textBox1.Text += "r2 =! I2\r\n";
                    textBox1.Text += "Structure\r\n";
                    textBox1.Text += "e0->r1->e1;\r\n";
                    textBox1.Text += "e1->r2->e0;\r\n";
                    textBox1.Text += "Actions\r\n";
                    textBox1.Text += "O1=e0;\r\n";
                    textBox1.Text += "O2=e1;\r\n";
                }
            }
        }


        private void textBox1_DoubleClick(object sender, EventArgs e)
        {
            /* Scanner scanner = new Scanner();
             Parser parser = new Parser(scanner);
             TextHighlighter highLighter = new TextHighlighter(textBox1, scanner, parser);*/
        }

        private void button2_Click(object sender, EventArgs e)
        {
            autoSave();
            generation generer = new generation();

            generer.generer(textBox2.Text, "", "", richTextBox1);
            //System.Diagnostics.Process.Start(@"C:\DOCUME~1\usrlocal\Bureau\arduino-1.0.3\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=103 -IC:\DOCUME~1\usrlocal\Bureau\arduino-1.0.3\hardware\arduino\cores\arduino -IC:\DOCUME~1\usrlocal\Bureau\arduino-1.0.3\hardware\arduino\variants\standard C:\DOCUME~1\usrlocal\Bureau\compilation\tableau_pde.cpp -o C:\DOCUME~1\usrlocal\Bureau\compilation\tableau_pde.cpp.o");
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            /*Scanner scanner = new Scanner();
            Parser parser = new Parser(scanner);
            TextHighlighter highLighter = new TextHighlighter(textBox1, scanner, parser);*/
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (BoolAffichage == true)
            {
                int i = 0;
                foreach (Branche B in Document.Doc._listeLesBranches)
                {
                    draw.SetGraphics(e.Graphics);
                    draw.DessineBranche(B, ref i);

                }


            }



        }

        private void ZoomP_Click(object sender, EventArgs e)
        {
            draw.SetZoom(1);
            Refresh();
            Repositionner.Enabled = true;
        }

        private void ZoomM_Click(object sender, EventArgs e)
        {
            draw.SetZoom(-1);
            Refresh();
            Repositionner.Enabled = true;
        }



        private void buttonAff_Click(object sender, EventArgs e)
        {

            if (BoolAffichage == false)
            {
                BoolAffichage = true;
                buttonAff.Text = "Cacher";
            }
            else
            {
                BoolAffichage = false;
                buttonAff.Text = "Afficher";
            }
            Refresh();
        }

        private void parDéfautToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (File.Exists("code.txt"))
            {
                textBox1.Text = File.ReadAllText("code.txt");
            }
            else
            {
                textBox1.Text = "";
                textBox1.Text += "Etape e0:INIT e1\r\n";
                textBox1.Text += "Entree I1(PC.5):HI I2(PC.4):HI\r\n";
                textBox1.Text += "Sortie O1(PD.6) O2(PD.4)\r\n";
                textBox1.Text += "Timer T1/e0/200\r\n";
                textBox1.Text += "Receptivite \r\n";
                textBox1.Text += "r1 =! I1 & T1.OFF\r\n";
                textBox1.Text += "r2 =! I2\r\n";
                textBox1.Text += "Structure\r\n";
                textBox1.Text += "e0->r1->e1;\r\n";
                textBox1.Text += "e1->r2->e0;\r\n";
                textBox1.Text += "Actions\r\n";
                textBox1.Text += "O1=e0;\r\n";
                textBox1.Text += "O2=e1;\r\n";
            }
        }

        private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = File.ReadAllText(openFileDialog1.FileName);
            }
        }

        private void sauvegarderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                StreamWriter pathWriter = File.CreateText(saveFileDialog.FileName);
                pathWriter.Write(textBox1.Text);
                pathWriter.Close();
            }
        }

        private void autoSave()
        {
            StreamWriter pathWriter = File.CreateText("lastCode.txt");
            pathWriter.Write(textBox1.Text);
            pathWriter.Close();
        }

        private void Codage_FormClosing(object sender, FormClosingEventArgs e)
        {
            autoSave();
        }

        private void droite_Click(object sender, EventArgs e)
        {
            foreach (Branche B in Document.Doc._listeLesBranches)
            {
                B.DeplacerDroite();                
            }
            Refresh();
            Repositionner.Enabled = true;
        }

        private void monte_Click(object sender, EventArgs e)
        {
            foreach (Branche B in Document.Doc._listeLesBranches)
            {
                B.DeplacerMonte();
            }
            Refresh();
            Repositionner.Enabled = true;
        }

        private void descend_Click(object sender, EventArgs e)
        {
            foreach (Branche B in Document.Doc._listeLesBranches)
            {
                B.DeplacerDescend();
            }
            Refresh();
            Repositionner.Enabled = true;
        }

        private void gauche_Click(object sender, EventArgs e)
        {
            foreach (Branche B in Document.Doc._listeLesBranches)
            {
                B.DeplacerGauche();
            }
            Refresh();
            Repositionner.Enabled = true;
        }

        private void Repositionner_Click(object sender, EventArgs e)
        {
            foreach (Branche B in Document.Doc._listeLesBranches)
            {
                B.ResetValeur();
            }
            Document.Doc.PositionnerBranches();
            draw.ReSetZoom();
            Refresh();
            Repositionner.Enabled = false;
        }
    }
}
